package artibella.utils 
{
	import flash.display.DisplayObject;
	import flash.filters.BitmapFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;

	/**
	 * @author philippe bordachar
	 */
		
	public class FiltersUtils 
	{
		public static const CYAN:String = "cyan";
		public static const MAGENTA:String = "magenta";
		public static const YELLOW:String = "yellow";
		public static const BLACK:String = "black";
		
		private static var _tabC:Array = [ 0, 0, 0, 0, 0, 0, 158, 0, 0, 0, 0, 0, 224, 0, 0, 0, 0, 0, 1, 0 ];
		private static var _tabM:Array = [ 226, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 122, 0, 0, 0, 0, 0, 1, 0 ];
		private static var _tabY:Array = [ 255, 0, 0, 0, 0, 0, 237, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ]	
		private static var _tabB:Array = [ 26, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 1, 0 ];
		
		public function FiltersUtils()
		{
			throw new Error( "FiltersUtils : bad use" );
		}
		
		///////  S T A T I C
		
		public static function setGlow( obj:DisplayObject, c:int = 0xFF0000, a:Number = 1, blurX:int = 4, blurY:int = 4, strength:int = 2, inner:Boolean = false, knockout:Boolean = false ):void 
		{
			var g:GlowFilter = new GlowFilter( c, a, blurX, blurY, strength, 1, inner, knockout);
			_setFilter( obj, g );
		}
		
		public static function setDropShadow( obj:DisplayObject, distance:Number = 1, angleD:Number = 45, c:int = 0, a:Number = 1, blur:Number = 0, strength:Number = 1 ):void 
		{
			var s:DropShadowFilter = new DropShadowFilter( distance, angleD, c, a, blur, blur, strength);
			_setFilter( obj, s );
		}
		
		public static function setColorMatrix( obj:DisplayObject, c:int, a:Number = 1 ):void 
		{
			var m:Array = _getMatrix( c, a );
			var cm:ColorMatrixFilter = new ColorMatrixFilter( m );
			_setFilter( obj, cm );
		}
		
		// - - -
		
		public static function colorise( obj:DisplayObject, clr:String ):void
		{
			var m:Array = new Array(); 
			
			switch( clr ) {
				case CYAN : 
					m = _tabC; 
					break;
				case MAGENTA : 
					m = _tabM; 
					break;
				case YELLOW : 
					m = _tabY; 
					break;
				case BLACK : 
					m = _tabB; 
					break;
				default :
					trace( "This method applies primary colors only" );
					return;
			}
			
			var f:ColorMatrixFilter = new ColorMatrixFilter( m );
			obj.filters = [ f ];
		}
		
		
		public static function uncolorize( obj:DisplayObject ):void
		{
			var m:Array = new Array(); 
			
			m = m.concat( [ .5, .5, .5, 0, 0 ] );
			m = m.concat( [ .5, .5, .5, 0, 0 ] );
			m = m.concat( [ .5, .5, .5, 0, 0 ] );
			m = m.concat( [ 0, 0, 0, 1, 0 ] );
			
			var f:ColorMatrixFilter = new ColorMatrixFilter( m );
			obj.filters = [ f ];
		}
		
		///////  U T I L S
		
		private static function _setFilter( obj:DisplayObject, f:BitmapFilter ):void 
		{
			var effect:Array = obj.filters;
			effect.push( f );
			obj.filters = effect;
		}
		
		private static function _getMatrix( c:int, a:Number ):Array 
		{
			var sColor:String = c.toString( 16 );
			while (sColor.length < 6) { 
				sColor = "0" + sColor; 
			}

			var r:Number = int( "0x" + sColor.substr( 0, 2 ) ) / 0xFF;
			var g:Number = int( "0x" + sColor.substr( 2, 2 ) ) / 0xFF;
			var b:Number = int( "0x" + sColor.substr( 4, 2 ) ) / 0xFF;
			
			var m:Array =	new Array();
			m = m.concat( [ r, 0, 0, 0, 0 ] );
			m = m.concat( [ 0, g, 0, 0, 0 ] );
			m = m.concat( [ 0, 0, b, 0, 0 ] );	
			m = m.concat( [ 0, 0, 0, a, 0 ] );
			
			return m;
		}
	}
}